<style>
    .form-copy {
        margin-right: 5px;
        border-radius: 0;
        box-shadow: none;
        padding: 4px 12px 7px 12px;
        font-size: 14px;
        line-height: 1.42857143;
        color: #555;
        border: 1px solid #ccc;
        transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
    }
    .select2 {
        margin-right: 5px
    }
</style>
@if(\Encore\Admin\Facades\Admin::user()->can('os-in.os.receive-ex-in') || \Encore\Admin\Facades\Admin::user()->can('os-in.os.receive-ex-pay'))
{{-- 行选操作 start --}}
<input type="checkbox" class="grid-select-all" />&nbsp;
<div class="btn-group">
    <a class="btn btn-sm btn-default">  {{ trans('admin.action') }}</a>
    <button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
        <span class="caret"></span>
        <span class="sr-only">Toggle Dropdown</span>
    </button>
    <ul class="dropdown-menu" role="menu" style="position: absolute;z-index: 10000;min-width: 80px;">
        <li><a href="#" class="grid-batch-paid">赔付</a></li>

        <!--管理员入库 (海外仓代发入库)-->
        @if(\Encore\Admin\Facades\Admin::user()->id == 1)
            <li><a href="#" class="grid-batch-receive">入库</a></li>
        @endif
    </ul>
</div>
{{-- 行选操作 end --}}
@endif
<div class="btn-group" style="margin-right: 10px">
    <form action="{{ $action }}" pjax-container method="get">
        <input type="text" class="form-copy" style=" width:150px;" name="delivery_no" value="{{ $filtersBlade['delivery_no'] }}" placeholder="发货单号">
        <input type="text" class="form-copy" style=" width:150px;" name="batch_no" value="{{ $filtersBlade['batch_no'] }}" placeholder="批次号">
        <select id="warehouse_id" name="warehouse_id">
            {{ $filtersBlade['warehouse_id'] }}
        </select>
        <select id="account_id" name="account_id">
            {{ $filtersBlade['account_id'] }}
        </select>
        <select id="area_id" name="area_id">
            {{ $filtersBlade['area_id'] }}
        </select>
        <select id="classify_id" name="classify_id">
            {{ $filtersBlade['classify_id'] }}
        </select>
        <select id="status" name="status">
            {{ $filtersBlade['status'] }}
        </select>
        <select id="ex_deal" name="ex_deal">
            {{ $filtersBlade['ex_deal'] }}
        </select>
        <input type="text" class="form-copy" style="width:150px;" name="fnsku" value="{{ $filtersBlade['fnsku'] }}" placeholder="FNSKU">
        <input type="text" class="form-copy" style="width:150px;" name="sku" value="{{ $filtersBlade['sku'] }}" placeholder="SKU">
        <input style="width: 110px;" type="text" id="start_date" name="start_date" value="{{ $filtersBlade['start_date'] }}" class="form-copy" placeholder="发货开始日期">
        <input style="width: 110px;" type="text" id="end_date" name="end_date" value="{{ $filtersBlade['end_date'] }}" class="form-copy" placeholder="发货截止日期">
        <button class="btn btn-sm btn-info submit"><i class="fa fa-search"></i>&nbsp;&nbsp;{{ trans('admin.search') }}</button>
        <a style="margin-left: 10px;" href="{{ $action }}" class="btn btn-sm btn-default"><i class="fa fa-undo"></i>&nbsp;&nbsp;{{ trans('admin.reset') }}</a>
    </form>
</div>
<script language="JavaScript" type="text/javascript">
    $(function () {
        $('#warehouse_id').select2({placeholder: '目的仓库', allowClear: true, width: '150px'});
        $('#account_id').select2({placeholder: '账号', allowClear: true, width: '120px'});
        $('#area_id').select2({placeholder: '发货站点', allowClear: true, width: '100px'});
        $('#classify_id').select2({placeholder: '品类', allowClear: true, width: '120px'});
        $('#status').select2({placeholder: '状态', allowClear: true, width: '120px'});
        $('#ex_deal').select2({placeholder: '异常', allowClear: true, width: '120px'});
        $('#start_date').datetimepicker({"format":"YYYY-MM-DD","locale":"zh-CN","maxDate":moment()});
        $('#end_date').datetimepicker({"format":"YYYY-MM-DD","locale":"zh-CN","maxDate":moment()});

        $('.grid-select-all').iCheck({checkboxClass:'icheckbox_minimal-blue'});
        $('.grid-select-all').on('ifChanged', function(event) {
            if (this.checked) {
                $('.grid-row-checkbox').iCheck('check');
            } else {
                $('.grid-row-checkbox').iCheck('uncheck');
            }
        });

        var checkRows = function () {
            var errorRows = [];
            $('.grid-row-checkbox:checked').each(function(index){
                var tr = $(this).closest('tr');
                var batchNo = tr.find('input[name="batch_no"]').val();
                var boxNo = tr.find('input[name="box_no"]').val();
                var obj = tr.find('input[name="this_storage_num"]');
                var storageNum = obj.val();console.log(storageNum);
                var storageNumMax = obj.data('value');console.log(storageNumMax);
                if(Number(storageNum) <= 0 || Number(storageNum) > Number(storageNumMax)){
                    errorRows.push(batchNo+'-'+boxNo+"号箱");
                }
            });
            return errorRows;
        };

        var selectedRows = function () {
            var param = {};
            var selected = [];
            var detail = {};
            $('.grid-row-checkbox:checked').each(function(index){
                selected.push($(this).data('id'));
                var tr = $(this).closest('tr');
                var indexArr = {};
                indexArr['detail_key'] = tr.find('input[name="detail_key"]').val();
                indexArr['this_storage_num'] = tr.find('input[name="this_storage_num"]').val();
                detail[index] = indexArr;
            });
            param.selected = selected;
            param.detail = detail;
            return param;
        };

        $('.grid-batch-receive').unbind('click').click(function(e) {
            e.preventDefault();
            var param = selectedRows();
            var ids = param.selected;
            if(ids.length == 0) {
                swal('请选择入库单据', '', 'error');
                return false;
            }
            var errorRows = checkRows();
            if(errorRows.length > 0) {
                swal(errorRows.join('<br/>')+'<br/>箱数有误', '', 'error');
                return false;
            }
            var detail = param.detail;
            swal({
                title: "确定入库吗？",
                text : "确定按录入的数据入库吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        $.ajax({
                            method: 'post',
                            url: '/xqc/overseas-in/dropshipping-receive-ex-ajax',
                            data: {
                                _token:"{{ csrf_token() }}",
                                action:'batchReceive',
                                param:detail
                            },
                            success: function (data) {
                                resolve(data);
                            }
                        });
                    });
                }
            }).then(function(result) {
                var data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        $.pjax.reload('#pjax-container');
                        toastr.success('提交成功 !');
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });

        var selectedPaidRows = function () {
            var param = {};
            var selected = [];
            var detail = {};
            $('.grid-row-checkbox:checked').each(function(index){
                selected.push($(this).data('id'));
                var tr = $(this).closest('tr');
                var indexArr = {};
                indexArr['detail_key'] = tr.find('input[name="detail_key"]').val();
                detail[index] = indexArr;
            });
            param.selected = selected;
            param.detail = detail;
            return param;
        };

        $('.grid-batch-paid').unbind('click').click(function(e) {
            e.preventDefault();
            var param = selectedPaidRows();
            var ids = param.selected;
            if(ids.length == 0) {
                swal('请选择单据', '', 'error');
                return false;
            }
            var detail = param.detail;
            swal({
                title: "确定已赔付处理吗？",
                text : "确定按已赔付处理吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        $.ajax({
                            method: 'post',
                            url: '/xqc/overseas-in/dropshipping-receive-ex-ajax',
                            data: {
                                _token:"{{ csrf_token() }}",
                                action:'batchPaid',
                                param:detail
                            },
                            success: function (data) {
                                resolve(data);
                            }
                        });
                    });
                }
            }).then(function(result) {
                var data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        $.pjax.reload('#pjax-container');
                        toastr.success('提交成功 !');
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });
    });
</script>
